<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/admin/adminPage/AdminIndex' }">
      <i class="el-icon-s-promotion"></i> 后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item>添加景点攻略</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

  <el-form style="padding-left: 150px" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="标题" prop="title">
      <el-input style="width: 350px" v-model="ruleForm.title"></el-input>
    </el-form-item>
    <el-form-item label="摘要" prop="body">
      <el-input style="width: 350px" v-model="ruleForm.body"></el-input>
    </el-form-item>
    <el-form-item label="图片链接" prop="imgUrl">
      <el-input style="width: 350px" v-model="ruleForm.imgUrl"></el-input>
    </el-form-item>
    <el-form-item>
      <img width="50%" :src=ruleForm.imgUrl>
    </el-form-item>
    <el-form-item label="正文链接" prop="href">
      <el-input style="width: 350px" v-model="ruleForm.href"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  data(){
    return{
      ruleForm:{},
      rules:{
        title: [{required: true, message: '请输入标题', trigger: 'blur'}],
        body:[{required: true, message: '请输入摘要', trigger: 'blur'}],
        imgUrl: [{required: true, message: '请输入图片外链', trigger: 'blur'}],
        href: [{required: true, message: '请输入正文外链', trigger: 'blur'}]
      }
    };
  },
  methods:{
    submitForm(formName) {
          let url = 'http://localhost:9580/secondaryConsult/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('aJwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加咨询成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
    },
  resetForm(formName) {
    this.$refs[formName].resetFields();
  }
}
}
</script>

<style scoped>

</style>